package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.google.gson.Gson;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.YhdBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.SwipeRefreshHelper;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 隐患点信息查询
* */
public class XxcxActivity extends BaseActivity {
    RelativeLayout layout_back;
    TextView txt_title,txt_right;
    RecyclerView recyclerView;
    BaseRecyclerAdapter<YhdBean> baseRecyclerAdapter;
    private Dialog mWeiboDialog;
    List<YhdBean> YhdBeanlist=new ArrayList<>();
    List<String> lxBeans=new ArrayList<>();
    private String LeiXing="泥石流";
    private SwipeRefreshLayout swipregresh;
    private void setnet(String lx, boolean IsDialog) {
        if (YhdBeanlist.size()>0){
            YhdBeanlist.clear();
        }
        if (IsDialog) {
            mWeiboDialog = WeiboDialogUtils.
                    createLoadingDialog(XxcxActivity.this,
                            "加载中...");
        }
        Map<String,String> map=new HashMap<>();
        map.put("lx",lx);
        WebServiceUtils.callWebService(Constances.webhosturl, "N09_GetYHDList", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                if (IsDialog) {
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                }
                finishRefresh();
                try {
                    JSONArray array=new JSONArray(result.getProperty(0).toString());
                    Gson gson=new Gson();
                    for (int i=0;i<array.length();i++){
                        YhdBean qfqcBean=gson.fromJson(array.optJSONObject(i).toString(),YhdBean.class);
                        YhdBeanlist.add(qfqcBean);
                    }
                        baseRecyclerAdapter.setData(YhdBeanlist);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xxcx);
        layout_back= (RelativeLayout) findViewById(R.id.layout_back);
        txt_title= (TextView) findViewById(R.id.txt_title);
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        txt_right= (TextView) findViewById(R.id.txt_right);
        layout_back.setVisibility(View.VISIBLE);
        txt_title.setText("隐患点信息查询");
        txt_right.setText("隐患点类型");
        txt_right.setVisibility(View.VISIBLE);
        swipregresh= (SwipeRefreshLayout) findViewById(R.id.swipregresh);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initdata();

        lxBeans.add("泥石流");
        lxBeans.add("崩塌");
        lxBeans.add("采空塌陷");
        lxBeans.add("岩溶塌陷");

        txt_right.setText("泥石流");
        setnet(LeiXing,true);
        initSwipeRefresh();

    }
    protected void initdata() {
       baseRecyclerAdapter=new BaseRecyclerAdapter<YhdBean>(this,null,R.layout.item_xxcxview) {
           @Override
           protected void convert(BaseViewHolder helper,
                                  final YhdBean item) {

               helper.setText(R.id.content,item.getLX());//灾害详情
               helper.setText(R.id.time,item.getTIME());//时间
               helper.setText(R.id.title,item.getTITLE());//灾害点
               helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       Intent intent=new Intent(XxcxActivity.this,XxcxInfoActivity.class);
                       intent.putExtra("title",item.getTITLE());
                       intent.putExtra("txt",item.getLX());
                       intent.putExtra("time",item.getTIME());
                       intent.putExtra("ID",item.getID());
                       startActivity(intent);
                   }
               });

           }
       };
        baseRecyclerAdapter.openLoadAnimation(false);
        recyclerView.setAdapter(baseRecyclerAdapter);
//        setnet();

    }

    public void onclick(View view){
        switch (view.getId()){
            case R.id.layout_back:
                finish();
                break;
            case R.id.txt_right:
                OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        txt_right.setText(lxBeans.get(options1));
                        LeiXing=lxBeans.get(options1);
                        setnet(LeiXing,true);
                }
                })
                        .setTitleText("类型选择")
                        .setContentTextSize(20)//设置滚轮文字大小
                        .setSelectOptions(0, 0)//默认选中项
                        .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                        .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                        .setLabels("", "", "")
                        .build();

                pvOptions.setPicker(lxBeans);//一级选择器
                pvOptions.show();
                break;
        }
    }
    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (swipregresh != null) {
            SwipeRefreshHelper.init(swipregresh, new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    setnet(LeiXing,false);

                }
            });
        }
    }

    /**
     * 关闭刷新
     */
    public void finishRefresh() {
        if (swipregresh != null) {
            swipregresh.setRefreshing(false);
        }
    }
}
